package com.nf.health.app.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.HeadListView;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.inquiry.docdetail.DocDetailActivity;
import com.nf.health.app.models.DoctorVo;
import com.nf.health.app.mydoctor.adapter.GzDoctorAdapter;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ImageLoaderUtils;

import java.util.ArrayList;
import java.util.List;

/*
*
* 健康助手适配器
*/

public class HealthAdapter extends BaseAdapter implements SectionIndexer, HeadListView.HeaderAdapter, AbsListView.OnScrollListener {


	protected static final String TAG = GzDoctorAdapter.class.getSimpleName();
	private Activity context;
	private List<DoctorVo> doctors;
	public ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

	public HealthAdapter(Activity context, List<DoctorVo> doctors) {
		this.context = context;
		this.doctors = doctors;

	}

	public void notifyDataRefreshed(List<DoctorVo> doctors) {
		this.doctors = doctors;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (doctors != null) {
			return doctors.size();
		}
		return 10;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_my_health, position);
		ImageView iv_user_head_icon = holder.getView(R.id.iv_user_head_icon);
		ImageLoaderUtils.getinstance(context).getImage(iv_user_head_icon, doctors.get(position).getDoctorhimg(), R.drawable.default_head);

		if(TextUtils.isEmpty(doctors.get(position).getDoctorname())||doctors.get(position).getDoctorname()==null||doctors.get(position).getDoctorname().equals("")){
			holder.setText(R.id.doctor_name, doctors.get(position).getNick());
		}else{
			holder.setText(R.id.doctor_name, doctors.get(position).getDoctorname());//医生名
		}

		holder.setText(R.id.doctor_hospitalname, doctors.get(position).getHospitalname());//位置
//		holder.setText(R.id.doctor_hospitalname, doctors.get(position).getHospitalname()+" - "+doctors.get(position).getDepartment());
		holder.setText(R.id.doctor_goodremedy, "擅长:"+doctors.get(position).getGoodremedy());//擅长

		TextView btn_foucus_zixun =holder.getView(R.id.btn_foucus_zixun);

		btn_foucus_zixun.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(position,"zxan");
			}
		});

		SwipeView swipeView  = holder.getView(R.id.swipeView);
		View ll_content  = holder.getView(R.id.ll_content);
		View delete  = holder.getView(R.id.delete);
		delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(position,doctors.get(position).getDoctorid());
				unClosedSwipeView.clear();
			}
		});


		ll_content.setTag(position);
		ll_content.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (unClosedSwipeView.size() > 0) {
					closeAllOpenedSwipeView();
				} else {
					Bundle bundle = new Bundle();
					bundle.putString(DocDetailActivity.DOCTORID,doctors.get(position).getDoctorid());
					ActivityUtils.showActivity(context, DocDetailActivity.class, bundle);
				}
			}
		});

		swipeView.setOnSwipeStatusChangeListener(new SwipeView.OnSwipeStatusChangeListener() {

			@Override
			public void onSwiping(SwipeView swipingSwipeView) {
				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					closeAllOpenedSwipeView();
				}

				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					unClosedSwipeView.add(swipingSwipeView);
				}

			}

			@Override
			public void onOpen(SwipeView openedSwipeView) {
				for (int i = 0; i < unClosedSwipeView.size(); i++) {
					if (unClosedSwipeView.get(i) != openedSwipeView) {
						unClosedSwipeView.get(i).close();
					}
				}
				if (!unClosedSwipeView.contains(openedSwipeView)) {
					unClosedSwipeView.add(openedSwipeView);
				}
			}

			@Override
			public void onClose(SwipeView closedSwipeView) {
				unClosedSwipeView.remove(closedSwipeView);
			}
		});


		return holder.getConvertView();
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}
	//滑动监听
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
						 int visibleItemCount, int totalItemCount) {
	}

	@Override
	public int getHeaderState(int position) {
		int realPosition = position;
		if (realPosition < 0 || position >= getCount()) {
			return HEADER_GONE;
		}
		int section = getSectionForPosition(realPosition);
		int nextSectionPosition = getPositionForSection(section + 1);
		if (nextSectionPosition != -1
				&& realPosition == nextSectionPosition - 1) {
			return HEADER_PUSHED_UP;
		}
		return HEADER_VISIBLE;
	}
	@Override
	public void configureHeader(View header, int position, int alpha) {
		// TODO Auto-generated method stub

	}
	@Override
	public Object[] getSections() {
		return null;
	}
	@Override
	public int getPositionForSection(int section) {
		return 0;
	}
	@Override
	public int getSectionForPosition(int position) {
		return 0;
	}

	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){
		this.listener = listener;
	}
	private void closeAllOpenedSwipeView() {
		for (int i = 0; i < unClosedSwipeView.size(); i++) {
			if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
				unClosedSwipeView.get(i).close();
			}
		}
	}
}